using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace HadronData
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content
    /// Pipeline to read the specified data type from binary .xnb format.
    /// 
    /// Unlike the other Content Pipeline support classes, this should
    /// be a part of your main game project, and not the Content Pipeline
    /// Extension Library project.
    /// </summary>
    public class PolygonContentTypeReader : ContentTypeReader<Polygon>
    {
        protected override Polygon Read(ContentReader p_contentReader, Polygon p_polygon)
        {
            p_polygon = new Polygon();

            String strVertices = p_contentReader.ReadString();
            String[] strValues = strVertices.Split(' ');
            if (strValues.Length % 2 != 0)
                throw new FormatException("Invalid Vertices format: must consist of an even number of values.");

            int nCount = strValues.Length / 2;
            Vector2[] arrayVertices = new Vector2[nCount];
            for (int nIndex = 0; nIndex < nCount; nIndex++)
            {
                arrayVertices[nIndex] = new Vector2(float.Parse(strValues[nIndex * 2]), float.Parse(strValues[nIndex * 2 + 1]));
            }

            p_polygon.Vertices = arrayVertices;
            return p_polygon;
        }
    }
}
